<template>
  <div class="AddFile"></div>
</template>

<script>
export default {
  inject: ['setExpandedKeys',],
  props: ['addFileBox', 'nodeData',"fileUrl","keysForId"],
  data () {
    return {
      isAddFileVisi: ''
    }
  },
  created() {
    // console.log(this.fileUrl,this.keysForId);
    console.log('AddFile===addFileBox===', this.addFileBox);
    console.log('AddFile===nodeData==', this.nodeData);
    if (this.addFileBox.isAddFileVisible) {
      this.addFileFun(this.addFileBox.dirId)
    }
  },
  methods: {
    addFileFun(id) {
      console.log('addFileFun para',id);
      this.$prompt(" ", "新建文件夹", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        appendToBody: true,
        inputValidator: function(value) {
          let reg = new RegExp('^[^\\\\\\/:.*?\\"<>| ]+$');// 转义 \ 符号也不行
          let valid
          if (value !== null) {
            reg.test(value) ? valid = true : valid = false
          }
          return valid
        },
        inputErrorMessage: '命名格式不正确 (文件名不能包含空格及 【/.:*?\\"<>|】)'
      }).then(({ value }) => {
        if(value.length>20){
          console.log(value.length);
          this.$message({type: "info", message: "文件夹名长度超出上限"});
          return false;
        }
        this.$http.post("/file/mkdir", {
          // idPath:this.idPath,
          // namePath:this.namePath,
          dirId: id == undefined ? localStorage.getItem("dirId") : id,
          fileType: 1,
          fileName: value
        }).then(res => {
          console.log("addFile", res);
          if (res.status == 200) {
            // localStorage.setItem('addFileBox', this.addFileBox.isAddFileVisible)
            // this.setExpandedKeys(id)
            let para;
            let loaded = true;
            let newChildNode = {
              id: res.data,
              label: value,
              dirId: parseInt(id),
              type: 1,
              // children:[{}]
              // leaf: true
            }
            let oldNode = ''
            if (this.nodeData.currNode == false){
              oldNode = this.nodeData.nodeInfo
            }else {
              oldNode = this.nodeData.nodeInfo
            }
            oldNode.loaded = true
            para = {
             childNode: newChildNode,
             parentNode: oldNode,
             loaded: false
            }
            console.log('oldNode', this.nodeData);
            this.$emit('addNode', para)
            this.$message({
              type: "success",
              message: "新建成功"
            });
          }
        });
      })
    }
  }

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
